package com.pet.web.controller.system;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.pet.common.annotation.Log;
import com.pet.common.core.controller.BaseController;
import com.pet.common.core.domain.AjaxResult;
import com.pet.common.core.domain.entity.SysDictData;
import com.pet.common.enums.BusinessType;
import com.pet.system.service.ISysDictDataService;
import com.pet.web.controller.system.vo.CategoryBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 类目controller
 *
 * @author pet
 * @date 2022-10-09
 */
@Api(value = "类目管理", tags = "类目管理")
@RestController
@RequestMapping("/system/category")
public class PetCategoryController extends BaseController {
    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询类目列表
     */
    @ApiOperation("查询类目列表")
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public AjaxResult list() {
        List<SysDictData> list = dictDataService.selectDictDataByType("pet_category");
        JSONArray jsonArray = new JSONArray();
        list.forEach(e -> {
            JSONObject jsonObject = new JSONObject()
                    .fluentPut("categoryId", e.getDictValue())
                    .fluentPut("category", e.getDictLabel());
            jsonArray.add(jsonObject);
        });
        return AjaxResult.success(jsonArray);
    }

    /**
     * 新增类目
     */
    @ApiOperation("新增类目")
    @ApiImplicitParam(name = "categoryBody", value = "categoryBody", required = true, dataTypeClass = CategoryBody.class)
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @PostMapping
    public AjaxResult add(@RequestBody CategoryBody categoryBody) {
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("pet_category");
        sysDictData.setDictLabel(categoryBody.getCategory());
        List<SysDictData> sysDictDataList = dictDataService.selectDictDataByType("pet_category");
        Long maxSort = sysDictDataList.stream().map(SysDictData::getDictSort).max(Long::compare).get();
        sysDictData.setDictSort(maxSort + 1);
        sysDictData.setDictValue(String.valueOf(maxSort + 1));
        sysDictData.setCreateBy(getUsername());
        return toAjax(dictDataService.insertDictData(sysDictData));
    }

    /**
     * 删除类目
     */
    @ApiOperation("删除类目")
    @ApiImplicitParam(name = "categoryId", value = "categoryId", required = true)
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @DeleteMapping("/{categoryId}")
    public AjaxResult remove(@PathVariable("categoryId") String categoryId) {
        List<SysDictData> sysDictDataList = dictDataService.selectDictDataByType("pet_category");
        SysDictData sysDictData = sysDictDataList.stream().filter(e -> e.getDictValue().equals(categoryId)).findFirst().get();
        Long[] ids = {sysDictData.getDictCode()};
        return toAjax(dictDataService.deleteDictDataByIds(ids));
    }
}
